<template>
  <div class="app-container">
    <el-button icon="el-icon-plus" @click="handleAdd">新增</el-button>

    <el-table :data="scenicList" stripe style="width: 100%">
      <el-table-column prop="scenicName" label="景区名称"></el-table-column>
      <el-table-column prop="latitude" label="纬度"></el-table-column>
      <el-table-column prop="longitude" label="经度"></el-table-column>
      <el-table-column prop="price" label="价格"></el-table-column>
      <el-table-column prop="remark" label="二维码">
        <template slot-scope="scope">
          <el-image :src="scope.row.remark" fit="cover" style="width:100px;height:100px" :preview-src-list="[scope.row.remark]"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="updateTime" label="修改时间"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="editRow(scope.row)" type="text" size="small"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click.native.prevent="deleteRow(scope.row)"
            >删除</el-button
          >
          <el-button @click="getQRCodeFn(scope.row)" type="text" size="small">生成小程序码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="景区名称" prop="scenicName">
              <el-input
                v-model="form.scenicName"
                placeholder="请输入景区名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入纬度" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入经度" />
            </el-form-item>
          </el-col>
          <el-col>
            <el-button
              type="primary"
              @click="toAddress"
              style="margin-left: 80px; margin-bottom: 20px"
              >点击跳转到获取经纬度</el-button
            >
          </el-col>
          <!-- <el-col :span="24">
            <el-form-item label="语音讲解">
              <AudioUpload
                :parfileList="audioList"
                :getAudioList="getAudioList"
              ></AudioUpload>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="景区价格" prop="price">
              <el-input v-model="form.price" placeholder="请输入景区价格" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="24">
            <el-form-item label="景区介绍">
              <editor v-model="form.remark" :min-height="192" />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listScenic,
  addScenic,
  editScenic,
  getScenic,
  deleteScenic,
  getQRCode
} from "@/api/system/scenic";

export default {
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      loading: true,
      scenicList: [],
      queryParams: {
        language: 1,
      },
      activeName: 1,
      // 表单参数
      form: {},
      title: "",
      audioList: [],
      open: false,
      // 表单校验
      rules: {
        scenicName: [
          { required: true, message: "景区名称不能为空", trigger: "blur" },
        ],
        latitude: [
          { required: true, message: "景区纬度不能为空", trigger: "blur" },
        ],
        longitude: [
          { required: true, message: "景区经度不能为空", trigger: "blur" },
        ],
        price: [
          { required: true, message: "景区名称不能为空", trigger: "blur" },
        ],
        remark: [
          { required: true, message: "景区介绍不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    toAddress() {
      window.open("https://lbs.qq.com/tool/getpoint/get-point.html", "_blank");
    },
    getList() {
      this.loading = true;
      listScenic(this.queryParams).then((response) => {
        if (response) {
          this.scenicList = response.data;
          this.loading = false;
        }
      });
    },
    reset() {
      this.form = {
        id: undefined,
        scenicName: undefined,
        latitude: undefined,
        longitude: undefined,
        price: undefined,
      };
      this.audioList = [];
      this.resetForm("form");
    },
    editRow(row) {
      this.reset();
      const id = row.id;
      getScenic({ id: id }).then((response) => {
        this.form = response.data;
        this.audioList = response.data?.audioList;
        this.open = true;
        this.title = "修改景区介绍";
      });
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加景区介绍";
    },
    onChange(file, fileList) {
      this.audioList = fileList;
    },
    deleteRow(row) {
      const id = row.id;
      this.$modal
        .confirm("是否确认删除该景区？")
        .then(function () {
          return deleteScenic({ id: id });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let params = {
            ...this.form,
            audioList: this.audioList,
            language: Number(this.activeName),
          };
          if (this.form.id != undefined) {
            editScenic(params).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScenic(params).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    getAudioList(res) {
      this.audioList = res;
    },
    //s生成小程序码
    getQRCodeFn(row){
      getQRCode({id: row.id}).then(res => {
        this.$modal.msgSuccess("小程序码生成成功");
        this.getList();
      })
    },
  },
};
</script>

<style>
</style>